package com.busi.wms.controller;

import java.util.ArrayList;
import java.util.List;

import com.busi.wms.model.WmsMaterial;
import com.busi.wms.model.WmsOutstorageDetail;
import com.jfinal.plugin.activerecord.Record;
import com.zcurd.common.tool.DBTool;
import com.zcurd.common.util.StringUtil;
import com.zcurd.controller.BaseController;
import com.zcurd.ext.render.csv.CsvRender;

/**
 * 出库单明细表
 */
public class WmsOutstorageDetailController extends BaseController {
	
	public void listPage() {
		setAttr("outstorage_id",getPara("id"));
		setAttr("out_status",getPara("outstatus"));
		render("list.html");
	}
	public void listDetailPage() {
		setAttr("outstorage_id",getPara("id"));
		render("listDetail.html");
	}
	public void listData() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_outstorage_detail", properties, symbols, values, orderBy, getPager());
		
		renderDatagrid(
			list, 
			DBTool.countByMultProperties("wms_outstorage_detail", properties, symbols, values)
		);
	}	
	//增加页面
	public void addPage() {
		setAttr("outstorage_id",getPara("outstorage_id"));
		setAttr("dictDataunit_name", WmsMaterial.me.getDictDataunit_name());
		render("add.html");
	}
	
	//增加
	public void add() {
		getModel(WmsOutstorageDetail.class, "model").save();
		
		addOpLog("[出库单明细表] 增加");
		renderSuccess();
	}
	
	//修改页面
	public void updatePage() {
		setAttr("model", WmsOutstorageDetail.me.findById(getPara("id")));
		setAttr("dictDataunit_name", WmsMaterial.me.getDictDataunit_name());
		render("update.html");
	}
	
	//修改
	public void update() {
		WmsOutstorageDetail model = WmsOutstorageDetail.me.findById(getPara("id"));
		model.set("outstorage_id", getPara("model.outstorage_id"));
		model.set("mat_no", getPara("model.mat_no"));
		model.set("mat_name", getPara("model.mat_name"));
		model.set("mat_std", getPara("model.mat_std"));
		model.set("mat_unit", getPara("model.mat_unit"));
		model.set("mat_num", getPara("model.mat_num"));
		model.set("mat_price", getPara("model.mat_price"));
		model.set("mat_sum", getPara("model.mat_sum"));
		model.update();
		addOpLog("[出库单明细表] 修改");
		renderSuccess();
	}
	
	//删除
	public void delete() {
		Integer[] ids = getParaValuesToInt("id[]");
		for (Integer id : ids) {
			new WmsOutstorageDetail().set("id", id).delete();
			
		}
		
		addOpLog("[出库单明细表] 删除");
		renderSuccess();
	}
	
	//详情页面
	public void detailPage() {
		WmsOutstorageDetail model = WmsOutstorageDetail.me.findById(getParaToInt("id"));
		setAttr("model", model);
		render("detail.html");
	}
	
	//导出csv
	public void exportCsv() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_outstorage_detail", properties, symbols, values);
		
		List<String> headers = new ArrayList<String>();
		List<String> clomuns = new ArrayList<String>();
		headers.add("关联出库单号");
		clomuns.add("outstorage_id");
		headers.add("物料编号");
		clomuns.add("mat_no");
		headers.add("物料名");
		clomuns.add("mat_name");
		headers.add("规格");
		clomuns.add("mat_std");
		headers.add("单位");
		clomuns.add("mat_unit");
		headers.add("数量");
		clomuns.add("mat_num");
		headers.add("单价");
		clomuns.add("mat_price");
		headers.add("金额");
		clomuns.add("mat_sum");
		
		CsvRender csvRender = new CsvRender(headers, list);
		csvRender.clomuns(clomuns);
		csvRender.fileName("出库单明细表");
		
		addOpLog("[出库单明细表] 导出cvs");
		render(csvRender);
	}
}
